/*
 * Copyright (c) 2004-2025, University of Oslo
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice, this
 * list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation
 * and/or other materials provided with the distribution.
 *
 * 3. Neither the name of the copyright holder nor the names of its contributors 
 * may be used to endorse or promote products derived from this software without
 * specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package org.hisp.dhis.datavalue;

import static java.util.Objects.requireNonNull;

import java.util.Map;
import java.util.Objects;
import java.util.Set;
import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
import org.hisp.dhis.category.CategoryCombo;
import org.hisp.dhis.category.CategoryOption;
import org.hisp.dhis.category.CategoryOptionCombo;
import org.hisp.dhis.common.OpenApi;
import org.hisp.dhis.common.UID;
import org.hisp.dhis.csv.CSV;
import org.hisp.dhis.dataelement.DataElement;
import org.hisp.dhis.organisationunit.OrganisationUnit;

/** A single data value in the data entry service API. */
public record DataEntryValue(
    int index,
    // names are chosen to be aligned with web API input
    @Nonnull UID dataElement,
    @Nonnull UID orgUnit,
    @CheckForNull UID categoryOptionCombo,
    @CheckForNull UID attributeOptionCombo,
    @Nonnull String period,
    String value,
    String comment,
    Boolean followUp,
    Boolean deleted)
    implements DataEntryId {

  public DataEntryValue {
    requireNonNull(dataElement);
    requireNonNull(orgUnit);
    requireNonNull(period);
  }

  public DataEntryValue toDeleted() {
    return deleted == Boolean.TRUE
        ? this
        : new DataEntryValue(
            index,
            dataElement,
            orgUnit,
            categoryOptionCombo,
            attributeOptionCombo,
            period,
            value,
            comment,
            followUp,
            true);
  }

  public DataEntryKey toKey() {
    return new DataEntryKey(
        dataElement, orgUnit, categoryOptionCombo, attributeOptionCombo, period);
  }

  public DataEntryValue withValue(String newValue) {
    return Objects.equals(newValue, value)
        ? this
        : new DataEntryValue(
            index,
            dataElement,
            orgUnit,
            categoryOptionCombo,
            attributeOptionCombo,
            period,
            newValue,
            comment,
            followUp,
            deleted);
  }

  /**
   * How a {@link DataEntryValue} is provided as user input in the web API.
   *
   * <p>Mainly different from a {@link DataEntryValue} in that the {@link UID}s can also be other
   * identifiers when using ID-schemes. Also, some fields might not be present because they are
   * common for all values, so they are defined in the group (request).
   */
  @OpenApi.Shared(name = "DataEntryValue")
  public record Input(
      // names are chosen to be aligned with web API input
      @CheckForNull @OpenApi.Property({UID.class, DataElement.class}) String dataElement,
      @CheckForNull @OpenApi.Property({UID.class, OrganisationUnit.class}) String orgUnit,
      @CheckForNull @OpenApi.Property({UID.class, CategoryOptionCombo.class})
          String categoryOptionCombo,
      @OpenApi.Description(
              """
        An alternative to `categoryOptionCombo` defining which category option (value) is chosen for which category (key)
        for the category combo effective for the `dataElement`.
        Will only be considered if `categoryOptionCombo` is not present.""")
          @CheckForNull
          @CSV.Any(
              ifAbsent = "categoryOptionCombo",
              ignore = {"storedBy", "timestamp"})
          Map<String, String> categoryOptions,
      @CheckForNull @OpenApi.Property({UID.class, CategoryOptionCombo.class})
          String attributeOptionCombo,
      @CheckForNull @OpenApi.Property({UID.class, CategoryCombo.class}) String attributeCombo,
      @CheckForNull @OpenApi.Property({UID[].class, CategoryOption.class})
          Set<String> attributeOptions,
      @CheckForNull String period,
      @CheckForNull String value,
      @CheckForNull String comment,
      @CheckForNull Boolean followUp,
      @CheckForNull Boolean deleted) {

    public Input(
        String dataElement,
        String orgUnit,
        String categoryOptionCombo,
        String attributeOptionCombo,
        String period,
        String value,
        String comment) {
      this(
          dataElement,
          orgUnit,
          categoryOptionCombo,
          null,
          attributeOptionCombo,
          null,
          null,
          period,
          value,
          comment,
          null,
          null);
    }
  }
}
